package com.blog.admin.controller;

import com.blog.admin.feign.UploadFileService;
import com.blog.admin.service.ArticleService;
import com.blog.admin.service.TagService;
import com.blog.common.core.domain.R;
import com.blog.common.core.enums.PathEnum;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.domain.dto.ArticleDto;
import com.blog.common.domain.dto.ArticleStatusDto;
import com.blog.common.domain.dto.ArticleTopDto;
import com.blog.common.domain.dto.ConditionDto;
import com.blog.system.api.feign.RemoteFileService;
import com.blog.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 文章后台控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/3/12
 **/
@RestController
@Api("文章后台模块")
@RequiredArgsConstructor
public class ArticleBackController {


    private final ArticleService articleService;


    private final TagService tagService;


    private final UploadFileService uploadFileService;


    private final RemoteFileService remoteFileService;

    /**
     * 查看后台文章
     *
     * @param condition 条件
     * @return {@link AjaxResult} 后台文章列表
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/articles/list")
    public AjaxResult listArticleBacks(ConditionDto condition) {
        return AjaxResult.success(articleService.listArticleBacks(condition));
    }

    /**
     * 根据id查看后台文章
     *
     * @param articleId id
     * @return {@link AjaxResult} 后台文章列表
     */
    @ApiOperation(value = "根据id查看后台文章")
    @GetMapping("/articles/{articleId}")
    public AjaxResult getArticleBackById(@PathVariable("articleId") Serializable articleId) {
        return AjaxResult.success(articleService.getArticleBackById(articleId));
    }

    /**
     * 根据id顶置后台文章
     *
     * @param articleTopDto top
     * @return {@link AjaxResult} 后台文章列表
     */
    @ApiOperation(value = "根据id顶置文章")
    @PutMapping("/articles/top")
    public AjaxResult updateArticleBackTop(@RequestBody ArticleTopDto articleTopDto) {
        Boolean flag = articleService.updateArticleBackTop(articleTopDto);
        return flag ? AjaxResult.success() : AjaxResult.error("修改失败");
    }

    /**
     * 添加或修改文章
     *
     * @param articleDto  文章
     * @return AjaxResult
     */
    @PostMapping("/articles")
    @ApiOperation(value = "添加或修改文章")
    public AjaxResult saveOrUpdateArticle(@RequestBody ArticleDto articleDto){
        articleService.saveOrUpdateArticle(articleDto);
        return AjaxResult.success();
    }

    /**
     * 推荐文章
     *
     * @param articleStatusDto 文章状态
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation(value = "删除文章")
    @GetMapping("/articles/recommend")
    public AjaxResult recommendArticle(ArticleStatusDto articleStatusDto) {
        articleService.recommendArticle(articleStatusDto);
        return AjaxResult.success();
    }

    /**
     * 删除文章
     *
     * @param articleIds 文章id
     * @return {@link AjaxResult} 文章图片地址
     */
    @ApiOperation(value = "删除文章")
    @DeleteMapping("/articles/{articleIds}")
    public AjaxResult deleteArticle(@PathVariable("articleIds") String articleIds) {
        articleService.deleteArticle(articleIds);
        return AjaxResult.success();
    }

    /**
     * 上传文章图片
     *
     * @param file 文件
     * @return {@link AjaxResult} 文章图片地址
     */
    @ApiOperation(value = "上传文章图片")
    @ApiImplicitParam(name = "file", value = "文章图片", required = true, dataType = "MultipartFile")
    @PostMapping("/articles/images")
    public AjaxResult saveArticleImages(MultipartFile file) {
        return AjaxResult.success(remoteFileService.upload(PathEnum.ARTICLE, file).getData());
    }

    /**
     * 上传文章图片
     *
     * @param file 文件
     * @return {@link AjaxResult} 文章图片地址
     */
    @ApiOperation(value = "上传文章图片")
    @ApiImplicitParam(name = "file", value = "文章封面图片", required = true, dataType = "MultipartFile")
    @PostMapping("/articles/coverImages")
    public AjaxResult saveArticleCoverImages(MultipartFile file) {
        return AjaxResult.success(remoteFileService.upload(PathEnum.ARTICLE, file).getData());
    }

    /**
     * 上传文章图片
     * @param pathEnum 文件路径
     * @param file 文件
     * @return {@link AjaxResult} 文章图片地址
     */
    @ApiOperation(value = "上传文件")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    @PostMapping("/file/uploadFile")
    public R<SysFile> uploadFile(@RequestParam("pathEnum") PathEnum pathEnum, @RequestPart(value = "file") MultipartFile file) throws Exception {
        return R.ok(uploadFileService.uploadFile(pathEnum, file));
    }

}
